package com.KSA.smartpillowapp;



import java.util.Calendar;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.support.v7.app.ActionBarActivity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends IOIOActivity implements OnClickListener{

	
	private TextView tempDisplay;
	private TextView alarmDisplay;
	private Button refresh;
	private Button log;
	public  float temp1 = (float)0.00;
	public  float temp2 = (float)0.00;
	public  float temp3 = (float)0.00;
	public  float temp4 = (float)0.00;
	public  float temp5 = (float)0.00;
	
	public float maxTemptemp = 0;
	
	public boolean tempboo= true;
	
    Button btnStart;
	Context context;
	static PendingIntent pendingIntent;
	static AlarmManager alarm;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        tempDisplay = (TextView) findViewById(R.id.textView1);
        alarmDisplay = (TextView) findViewById(R.id.textView2);

		Intent intentsOpen = new Intent(this, AlarmReceiver.class);
		intentsOpen.setAction(".ACTION");
		pendingIntent = PendingIntent.getBroadcast(this,111, intentsOpen, 0);
		alarm = (AlarmManager) getSystemService(ALARM_SERVICE);


		btnStart=(Button)findViewById(R.id.button1);		
		btnStart.setOnClickListener(this);
    }
    
    class Looper extends BaseIOIOLooper{
    	
    	private AnalogInput tempFromPin1_;
    	private AnalogInput tempFromPin2_;
    	private AnalogInput tempFromPin3_;
    	private AnalogInput tempFromPin4_;
    	private AnalogInput tempFromPin5_;
    	private int boardPin1=41;
    	private int boardPin2=42;
    	private int boardPin3=43;
    	private int boardPin4=44;
    	private int boardPin5=45;
    	private float getVoltage1;
    	private float getVoltage2;
    	private float getVoltage3;
    	private float getVoltage4;
    	private float getVoltage5;
    	
    	
    	@Override
    	protected void setup() throws ConnectionLostException{
    		
    		tempFromPin1_=ioio_.openAnalogInput(boardPin1);
    		tempFromPin2_=ioio_.openAnalogInput(boardPin2);
    		tempFromPin3_=ioio_.openAnalogInput(boardPin3);
    		tempFromPin4_=ioio_.openAnalogInput(boardPin4);
    		tempFromPin5_=ioio_.openAnalogInput(boardPin5);
    	}
    	
    	@Override
		public void loop() throws ConnectionLostException{
    		
    		
    		try{
    			Thread.sleep(5000);
    			getVoltage1=tempFromPin1_.getVoltage();
    			System.out.println(getVoltage1);
    			getVoltage2=tempFromPin2_.getVoltage();
    			getVoltage3=tempFromPin3_.getVoltage();
    			getVoltage4=tempFromPin4_.getVoltage();
    			getVoltage5=tempFromPin5_.getVoltage();
    		}catch (InterruptedException e){
    			tempFromPin1_.close();
    			tempFromPin2_.close();
    			tempFromPin3_.close();
    			tempFromPin4_.close();
    			tempFromPin5_.close();
    			e.printStackTrace();
    		}
    		temp1=((getVoltage1*1024)-500)/10;
    		temp2=((getVoltage2*1024)-500)/10;
    		temp3=((getVoltage3*1024)-500)/10;
    		temp4=((getVoltage4*1024)-500)/100;
    		temp5=((getVoltage5*1024)-500)/100;
    		
    		float[] templist = new float[5];
    		templist[0]=temp1;
    		templist[1]=temp2;
    		templist[2]=temp3;
    		templist[3]=temp4;
    		templist[4]=temp5;
    		
    		float MaxTemp = templist[0];
    		for (int i=1;i<templist.length;i++){
    			if (MaxTemp<templist[i]){
    				MaxTemp = templist[i];
    			}
    				
    		}
    		maxTemptemp = MaxTemp;
    		
    		
    		
    		runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					
					if (tempboo==true & maxTemptemp>37){ 
	        			startAlarm();
	        			AlertDialog alertDialog = new AlertDialog.Builder(
	                            MainActivity.this).create();
	    				// Dialog Title
	    				alertDialog.setTitle("Smart Pillow Alert");

	    				// Dialog message
	    				alertDialog.setMessage("Warning! Temperature is beyond normal body temperature!");

	    				// Icon for Dialog
	    				alertDialog.setIcon(R.drawable.mark);

	    				// Setting OK Button
	    				alertDialog.setButton("OK",
	    						new DialogInterface.OnClickListener() {

	    							public void onClick(DialogInterface dialog,
	    									int which) {
	    								//dialog after alarm has started
	    								stopAlarm();
	    								Toast.makeText(getApplicationContext(),
	    										"Alarm has been stopped...", Toast.LENGTH_SHORT)
	    										.show();
	    							}
	    						});

	    				//show alert message
	    				alertDialog.show();

	        		}
					
					tempDisplay.setText(Float.toString(temp3));
				}
    		});
    	}
    }
    
	public void onClick(View v) {
   		if (tempboo==false){
   		tempboo=true;
   		//alarmDisplay.setText("Alarm is on");
   		}
   		else if(tempboo==true){
   			tempboo=false;
   			//alarmDisplay.setText("Alarm is off");
   		}
   	}

    	//}
    	public void startAlarm() {
    		/**
    		 * call broadcost reciver
    		 */
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTimeInMillis(System.currentTimeMillis());
    		alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000, pendingIntent);
    		
    		
    	}
    	public void stopAlarm(){
    				tempboo=false;
    				alarm.cancel(pendingIntent);
    		
    		
    		
    	}
	@Override
	protected IOIOLooper createIOIOLooper() {
		// TODO Auto-generated method stub
		return new Looper();
	}
			

    

}
